'use strict';
exports = module.exports = function ($scope, $rootScope, $timeout, $alert, $state, $uibModal, $API, $compile, $rule, throttleSubmit) {
    $scope.Rule = $rule;
    $scope.frm = {
        // 弹框表单项: 业务id关联 用户 推广
        adids: [],
    }
    $scope.frm2 = {
        userNameList: select2Config([], 1),
        bizNameList: select2Config([], 1),
        adNameList: select2Config([], 1),
        adidsAdd: function(){
            var isinvalid = $scope.frm2.f14Btn = (!$scope.frm.userName||!$scope.frm.bizName||!$scope.frm.adName)
            if( isinvalid ) { return; }
            for(var i = 0, l = $scope.frm.adids.length; i < l; i++){
                if( $scope.frm.adName == $scope.frm.adids[i].adid ){
                    $alert.error('请勿重复配置推广信息')
                    return;
                }
            }
            $scope.frm.adids.push({
                userName: $scope.frm2.userName.text,
                bizName: $scope.frm2.bizName.text,
                adName: $scope.frm2.adName.text,
                userId: $scope.frm.userName,
                adid: $scope.frm.adName
            });
            $scope.frm.userName = ''
            $scope.frm.bizName = ''
            $scope.frm.adName = ''
        },
        adidsDelete: function(index){
            $scope.frm.adids.splice(index, 1);
        },
    }
    var id = 12; // 获取到传参 比如$stateparam
    $API.Detail.monitorCnzzDetail({id:id})
    .then(function(data) {
        $timeout(function() {
            angular.extend($scope.frm, data)
        }, 0)
    });
    $API.Drop.customerGetList('search', {
    }).then(function(data){
        $timeout(function() {
            $scope.frm2.userNameList = data;
        }, 0) }
    );
    $API.Drop.businessGetB2classBusi('search', {
        type: '1,3'
    }).then(function(data){
        $timeout(function() {
            $scope.frm2.bizNameList = data;
        }, 0) }
    );
    $scope.doajax4Ad = function(){
        if( ! $scope.frm.userName && !$scope.frm.bizName){
            return;
        }
        $API.Drop.monitorCnzzBizAdid('search', {
            "userId":$scope.frm.userName,
            "bizId": $scope.frm.bizName
        }).then(function(data){
            $timeout(function() {
                $scope.frm2.adNameList = data;
            }, 0) }
        );
    }
    $scope.submitFun = throttleSubmit(function(){
        $scope.From.submitted = true;
        if( $scope.From.$invalid ) {
            $alert.error('请检查提交的信息')
            return false;
        }
        return $scope.frm
    },function(enable, param){
        console.log('提交代码', param)
        $timeout(function(){
            enable()
        },3e3)
    });
    $scope.cancelFun = function(){
        $alert.confirm("是否取消编辑？", function(close){
            // $go('app.make.maketable1');
            close();
        })
    };
}